package st.business.tepth.business.taskallocation;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import st.business.tepth.R;
import st.business.tepth.R2;
import st.business.tepth.business.bean.TaskAllocationBean;
import st.business.tepth.business.taskallocation.detail.AllocationDetailDelegate;
import st.business.tepth.business.taskallocation.modifytrouble.ModifyTroubleDelegate;
import st.business.tepth.business.taskallocation.returnfactory.ReturnFactoryDelegate;
import st.business.tepth.business.taskallocation.station.StationDelegate;
import st.business.tepth.manager.UserManager;
import st.shadow.library.core.base.mvp.MvpFragment;
import st.shadow.library.ui.dialog.DialogLoader;
import st.shadow.library.ui.dialog.IDialogClickListener;
import st.shadow.library.ui.recycler.BaseDecoration;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-02-26 10:30
 * 分配列表
 */

public class TaskAllocationDelegate extends MvpFragment<TaskAllocationPresenter> implements TaskAllocationContract.View {
    public static TaskAllocationDelegate newInstance() {
        return new TaskAllocationDelegate();
    }

    @BindView(R2.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R2.id.tv_tile)
    TextView mTvTitle;
    private PopupWindow popWindow;
    private TaskAllocationAdapter mAllocationAdapter;
    private InputMethodManager imms;
    private DialogLoader mDialogLoader;

    @Override
    public TaskAllocationPresenter getPresenter() {
        return new TaskAllocationPresenter(this);
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_task_allocation;
    }

    @OnClick({R2.id.tv_back})
    public void onBack() {
        pop();
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mTvTitle.setText("分配列表");
        mRecyclerView.setLayoutManager(new LinearLayoutManager(_mActivity));
        mRecyclerView.addItemDecoration(BaseDecoration.
                create(Color.parseColor("#f0f0f0"), 1));
        mAllocationAdapter = new TaskAllocationAdapter(R.layout.item_task_allocation, null);
        mRecyclerView.setAdapter(mAllocationAdapter);
        mAllocationAdapter.setEmptyView(R.layout.view_loading, (ViewGroup) mRecyclerView.getParent());
        mRecyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                TaskAllocationBean taskAllocationBean = (TaskAllocationBean) adapter.getItem(position);
                if (taskAllocationBean != null) {
                    if (view.getId() == R.id.tv_operation) {
                        start(AllocationDetailDelegate.newInstance(taskAllocationBean.getRepair_id()));
                    } else if (view.getId() == R.id.tv_modify_location) {
                        start(StationDelegate.newInstance(taskAllocationBean.getRepair_id()));
                    } else if (view.getId() == R.id.tv_modify_classification) {
                        start(ModifyTroubleDelegate.newInstance(taskAllocationBean.getRepair_id()));
                    } else if (view.getId() == R.id.tv_more) {
                        showPop(taskAllocationBean.getRepair_id(), taskAllocationBean.getSubFlag()
                                , taskAllocationBean.getTransferFlag(), view);
                    }
                }
            }
        });
        mDialogLoader = new DialogLoader(_mActivity);
    }


    @Override
    public void showAllocationList(List<TaskAllocationBean> list) {
        mAllocationAdapter.setEmptyView(R.layout.view_search_empty,
                (ViewGroup) mRecyclerView.getParent());
        mAllocationAdapter.setNewData(list);
    }

    @Override
    public void cancelReturnSucceed() {
        _mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mDialogLoader.stopProgressWithSuccess("取消转厂成功",
                        null, new IDialogClickListener() {
                            @Override
                            public void onClick() {
                                mDialogLoader.clearDialog();
                            }
                        });
            }
        });
    }

    @Override
    public void showError(final String msg) {
        _mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mDialogLoader.stopProgressWithFailed(msg, msg);

            }
        });
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        if (mAllocationAdapter != null) {
            mAllocationAdapter.setNewData(null);
            mAllocationAdapter.setEmptyView(R.layout.view_loading,
                    (ViewGroup) mRecyclerView.getParent());
        }
        mPresenter.getAllocationList(UserManager.getInstance().getToken());
    }

    private void showPop(final int repairId, int subFlag, final int transferFlag, View view) {
        if (popWindow != null && imms != null) {
            imms.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
        }
        if (popWindow == null) {
            imms = (InputMethodManager) _mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
            View layout = LayoutInflater.from(_mActivity).inflate(R.layout.view_pop_window, null);
            popWindow = new PopupWindow(layout, ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT, true);
            popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
            imms.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
            popWindow.setBackgroundDrawable(new ColorDrawable(0xffffffff));
            popWindow.setOutsideTouchable(true);

        }
        View contentView = popWindow.getContentView();
        if (contentView != null) {
            TextView tvF = contentView.findViewById(R.id.tv_return_factory);
            if (tvF != null) {
                if (subFlag == 0) {
                    //可转厂
                    if (transferFlag == 0) {
                        //未转厂
                        tvF.setText("转回主厂");
                    } else {
                        //已转厂
                        tvF.setText("取消转厂");
                    }
                    tvF.setEnabled(true);
                } else {
                    //不可转厂
                    tvF.setText("转回主厂");
                    tvF.setEnabled(false);
                }
                tvF.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popWindow.dismiss();
                        if (transferFlag == 0) {
                            start(ReturnFactoryDelegate.newInstance(repairId));
                        } else {
                            mDialogLoader.showProgressDialog(true, "正在提交",
                                    "请稍候...");
                            mPresenter.cancelReturn(UserManager.getInstance().getToken(),
                                    repairId);
                        }
                    }
                });
            }
        }
        if (!popWindow.isShowing()) {
            popWindow.showAsDropDown(view);
        } else {
            popWindow.dismiss();
        }
    }
}